enum EventNames {
  API_UN_AUTHORIZED = "API: UN_AUTHORIZED",
  API_INVALID = "API: INVALID",
  API_TIMEOUT = "API: TIMEOUT",
  API_NETWORK_ERROR = "API: NETWORK_ERROR",
  API_SESSION_EXPIRED = "API: SESSION_EXPIRED",
}

// 发布订阅模式
class EventEmiter {
  private listeners: Record<EventNames, Set<Function>> = {
    [EventNames.API_UN_AUTHORIZED]: new Set(),
    [EventNames.API_INVALID]: new Set(),
    [EventNames.API_TIMEOUT]: new Set(),
    [EventNames.API_NETWORK_ERROR]: new Set(),
    [EventNames.API_SESSION_EXPIRED]: new Set(),
  };

  // 监听事件
  on(eventname: EventNames, fn: Function) {
    this.listeners[eventname].add(fn);
  }

  // 触发事件
  emit(eventname: EventNames, ...args: any[]) {
    this.listeners[eventname].forEach((fn) => fn(...args));
  }
}

export { EventNames };
export default new EventEmiter();
